import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    ToolingRepairsServiceProxy,
    CostOfToolingRepairEditDto,
    CreateOrUpdateCostOfToolingRepairInput,
    ToolingServiceProxy,
    CostOfToolingListDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { CalendarModule } from 'primeng/calendar';
import { SelectItem } from 'primeng/api';

@Component({
    selector: 'createOrEditToolingRepairModal',
    templateUrl: './create-or-edit-tooling-repair-modal.component.html'
})
export class CreateOrEditToolingRepairModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _toolingRepairsServiceProxy: ToolingRepairsServiceProxy,
        private _toolingServiceProxy: ToolingServiceProxy
    ) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    fiter = '';
    sorting = '';

    //editDto: CostOfDeviceEditDto;
    //  extensions: Array<EditDtoExtensionsItem>;
    optionAll;
    editDto: CostOfToolingRepairEditDto = new CostOfToolingRepairEditDto();
    toolingDto: CostOfToolingListDto;
    toolingList = new Array<CostOfToolingListDto>();
    toolingSelectItem: SelectItem[];
    toolingId?: number;
    IdentifyType: SelectItem[];
    ngOnInit(): void {}
    show(id?: number): void {
        this.active = true;

        this._toolingServiceProxy.getAllToolingsAsync(this.fiter, this.sorting, 1000, 0).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
            this.toolingList = result.items;

            this.toolingSelectItem = [];
            for (let i = 0; i < this.toolingList.length; i++) {
                this.toolingSelectItem.push({
                    value: this.toolingList[i].id,
                    label:
                        '工装编号:' +
                        ' ' +
                        this.toolingList[i].toolingNumber +
                        ',' +
                        ' ' +
                        '工装名称:' +
                        ' ' +
                        this.toolingList[i].toolingName
                });
            }
        });

        this._toolingRepairsServiceProxy.getForEditAsync(id).subscribe(result => {
            this.editDto = result.costOfToolingRepair;
            this.toolingId = result.toolingId;
            const self = this;
            console.log(result.toolingId);
            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;
        let input = new CreateOrUpdateCostOfToolingRepairInput();
        input.costOfToolingRepair = this.editDto;
        input.toolingId = this.toolingId;
        console.log(input);
        this._toolingRepairsServiceProxy
            .createOrUpdateAsync(input)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
